<?php

/**
 * Terminal Test
 * php .\ethereum.php getTransactionByHash 0x01c5a8461d06c2c195035c148af0f871c7679841d86ae5bb98676bb2d8e68dfa
 * php .\ethereum.php getTransactionByAddress 0xdae787ec66e65c60ad35203800b97b45fcb0f909 1048576
 */
$arg1 = $argv[1];
$arg2 = $argv[2];
$arg3 = $argv[3];

switch ($arg1) {
    case "getTransactionByHash":
        $transaction = getTransactionByHash($arg2);
        var_dump($transaction);
    break;
    case "getTransactionByAddress":
        $transaction = getTransactionByAddress($arg2, $arg3);
        var_dump($transaction);
    break;
    default:
        var_dump("nothing to do");
}

function getTransactionByHash($hash) {
    require_once('./lib/clienteth.php');
    $ethclient = new EthClient("192.168.1.217", 8545);

    return $ethclient->eth_getTransactionByHash($hash);
}

function getTransactionByAddress($address, $startnumber) {
    require_once('./lib/clienteth.php');
    $ethclient = new EthClient("192.168.1.217", 8545);

    $endnumber = $ethclient->eth_blockNumber(true);
    if ($endnumber == 0) $endnumber = $startnumber + 1;
    $transactionlist = [];

    for ($i = $startnumber; $i <= $endnumber; $i++) {
        echo "blocknumber: " . $i . "\n";
        $block = $ethclient->eth_getBlockByNumber($ethclient->encode_dec($i), true);
        if ($block['transactions'] != NULL) {
            for ($k = 0; $k < count($block['transactions']); $k++) {
                $transactionlist[] = $block['transactions'][$k];
            }
        }
    }

    echo "\n\n\n";
    return $transactionlist;
}